<?php

class m_rights_actions_block_RightsManagementForGroupAction extends m_backoffice_extends_Action
{	
	public function execute()
	{	
		// Providers
		$documentProvider = f_document_Provider::getInstance();
		
		// Services
		$rightService = m_rights_services_RightsService::getInstance();
		$formService = f_form_FormService::getInstance();
		
		// Parameter
		$groupName = $this->getRequest()->getParameter('group');
		
		// Group doc
		$group = $documentProvider->loadDocuments("modules_users/group")->whereIsEqual("document_label",$groupName)->retrieveDocuments();
		$group = $group[0];
		
		// Group Rights
		$groupRights = $group->getIdright();
		$groupRights = $groupRights[0];

		// All modules name
		$modules = $rightService->getAllModulesName();
		
		// Set name of the form
		$formService->setFormName("rightsForm");
		
		// Get the form Params
		$values = $formService->getParams();

		// If form is submitted
		if($formService->isSubmitted())
		{
			// Construct a more confortable readable array(moduleName => rules) of rules checked in the form
			$toProcessed = array();
			foreach($values['formValues'] as $rule => $value)
			{
				$tmp = explode("/",$rule);
				if($tmp[0] != "submit")
				{
					$toProcessed[$tmp[0]][] = $tmp[1];
				}
			}
			
			// New rules list 
			$toWrite = array();
			
			// For all modules, we read the rights file and add or remove the checked rules
			foreach($modules as $module)
			{
				if($rightService->checkRightsFileExistForModule($module) == true)
				{
					// For each rules of the $module
					foreach($rightService->readRightsFromModule($module) as $therule)
					{
						// if some rules of the current module have been checked 
						if(isset($toProcessed[$module]) && in_array($therule['name'],$toProcessed[$module]) == true)
						{
							// Add it to the rule's list to save
							$toWrite[] = $module."_".$therule['name'];
						}
					}
				}
			}
			
			// Save the new rules for the $group		
			$rightService->saveRightsForGroup($group,$toWrite);
		}
		
		// ------- Read all rights files for the group
		$rights = array();
		foreach($modules as $moduleName)
		{
			if($rightService->checkRightsFileExistForModule($moduleName) == false)
			{
				$rights[$moduleName] = null;
			}
			else
			{
				$rights[$moduleName] = $rightService->readRightsFromModuleForGroup($moduleName,$group);
			}	
		}

		$values['rights'] = $rights;
		
		$this->setModel($values);
		$this->setView('success');
	}	
	
	public function validate()
	{
		$res = false;
		
		if($this->getRequest()->hasParameter('group'))
		{
			$documentProvider = f_document_Provider::getInstance();
			
			$groups = $documentProvider->loadDocuments("modules_users/group")->retrieveColumnValues("document_label");

			if(in_array(array($this->getRequest()->getParameter('group')),$groups))
			{
				$res = true;
			}
		}

		return $res;
	}
	
	public function errorHandler()
	{
		return null;
	}
}